import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        employeeIds: null,
        name: null
    }
}

const eInitPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        employeeIds: null,
        name: null
    }
}

const eTaskPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        name: null
    }
}
const numberPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        name: 'number',
        type: 'exam',
        employeeNumber: '',
        //用户名
        userName: '',
        // 机构名
        orgName: '',
        // 任务名称
        paperName: ''
    }
}
const citationPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        name: 'citation',
        type: 'exam',
        employeeNumber: '',
        //用户名
        userName: '',
        // 机构名
        orgName: '',
        // 任务名称
        paperName: ''
    }
}
// state
const state = {
    data: {
        examReports: [],
        eOrganizationReports: [],
        examReportTasks: [],
        numberExamTasks: [],
        citationExamTasks: []
    },
    ui: {
        page: deepCopy(initPage),
        ePage: deepCopy(eInitPage),
        eTaskPage: deepCopy(eTaskPage),
        citationPage: deepCopy(citationPage),
        numberPage: deepCopy(numberPage)
    }
}

// getters
const getters = {
    examReports: state => state.data.examReports,
    eOrganizationReports: state => state.data.eOrganizationReports,
    examReportTasks: state => state.data.examReportTasks,
    numberExamTasks: state => state.data.numberExamTasks,
    citationExamTasks: state => state.data.citationExamTasks,
    examReportsTotal: state => state.ui.page.total,
    eOrganizationTotal: state => state.ui.ePage.total,
    eTaskTotal: state => state.ui.eTaskPage.total,
    numberPage: state => state.ui.numberPage.total,
    citationPage: state => state.ui.citationPage.total
}

// actions
const actions = {
    // 学员报告
    getExamReports({ state, commit }, data) {
        api.getExamReports({
            loading: true,
            method: 'post',
            data: data ? data : state.ui.page,
            success: res => {
                const { pages, total } = res
                commit('SETEXAMREPORTSPAGE', { pages, total })

                // 字段处理
                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        item.user = item.trainee.username
                        item.name = item.trainee.name
                        item.numberId = item.trainee.employeeId
                        item.jobTitle = item.trainee.jobTitle
                        if (item.trainee.organizationTitles) {
                            item.institution = item.trainee.organizationTitles.join('/')
                        }
                        item.taskFinishedPercent =
                            item.businessCountOfAssigned === 0
                                ? 0
                                : (item.businessCountOfFinished / item.businessCountOfAssigned) * 100 + '%'
                        item.businessPassedPercent =
                            item.businessCountOfAssigned === 0
                                ? 0
                                : (item.businessCountOfPassed / item.businessCountOfAssigned) * 100 + '%'
                    }
                }
                commit('SETEXAMREPORTS', res.content)
            }
        })
    },
    // 组织报告
    getEOrganizationReports({ state, commit }, data) {
        api.getEOrganizationReports({
            loading: true,
            method: 'post',
            data: data ? data : state.ui.ePage,
            success: res => {
                const { pages, total } = res
                commit('SETEORGANIZATIONPAGE', { pages, total })

                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        item.organizationTitle = item.organization.title
                        item.taskCountOfAssigned = item.taskCountOfAssigned ? item.taskCountOfAssigned : 0
                        item.organizationOfPassed =
                            item.taskCountOfAssigned === 0
                                ? 0
                                : (item.taskCountOfFinished / item.taskCountOfAssigned) * 100 + '%'
                    }
                    commit('SETEORGANIZATIONREPORTS', res.content)
                }
            }
        })
    },

    // 学员分配的任务列表
    getExamReport({ state, commit }, data) {
        commit('SETTRAINEEEXAMREPORT', [])
        api.getExamReport({
            loading: true,
            method: 'post',
            data: data ? data : state.ui.eTaskPage,
            success: res => {
                const { pages, total } = res
                commit('SETETASKPAGE', { pages, total })

                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        Object.assign(item, item.task)
                        item.plainTime = `${item.startTime} ~ ${item.endTime ? item.endTime : 'N/A'}`
                    }
                    commit('SETTRAINEEEXAMREPORT', res.content)
                }
            }
        })
    },

    // 组织分配的任务
    getEOrganizationReport({ state, commit }, data) {
        commit('SETTRAINEEEXAMREPORT', [])
        api.getEOrganizationReport({
            loading: true,
            method: 'post',
            data: data ? data : state.ui.eTaskPage,
            success: res => {
                const { pages, total } = res
                commit('SETETASKPAGE', { pages, total })
                if (res.content && res.content.length) {
                    for (let item of res.content) {
                        item.plainTime = `${item.startTime} ~ ${item.endTime ? item.endTime : 'N/A'}`
                    }
                    commit('SETTRAINEEEXAMREPORT', res.content)
                }
            }
        })
    },

    // 清除员工考试报告
    deleteEmployeeExamReport({ dispatch }, params) {
        console.log('deleteEmployeeExamReport: ', params)
        api.deleteEmployeeExamReport({
            loading: true,
            method: 'post',
            data: params,
            success: () => {
                dispatch('getExamReports')
            }
        })
    },
    //
    againNumberExam({ commit }, params) {
        api.againNumberCitation({
            loading: true,
            method: 'post',
            data: params ? params : state.ui.numberPage,
            success: res => {
                const { pages, total } = res
                commit('SETEXAMNUMBERLISTPAGE', { pages, total })
                commit('SETEXAMNUMBERLIST', res.content)
            }
        })
    },
    againcitationExam({ commit }, params) {
        api.againNumberCitation({
            loading: true,
            method: 'post',
            data: params ? params : state.ui.citationPage,
            success: res => {
                const { pages, total } = res
                commit('SETEXCITATIONPAGE', { pages, total })
                commit('SETEXAMCITATIONLIST', res.content)
            }
        })
    }
}

// mutations
const mutations = {
    // 学员报告
    SETEXAMREPORTS(state, payload) {
        state.data.examReports = payload
    },
    // 组织报告
    SETEORGANIZATIONREPORTS(state, payload) {
        state.data.eOrganizationReports = payload
    },
    SETEXAMNUMBERLIST(state, payload) {
        state.data.numberExamTasks = payload
    },
    SETEXAMCITATIONLIST(state, payload) {
        state.data.citationExamTasks = payload
    },
    // 学员报告列表
    SETTRAINEEEXAMREPORT(state, payload) {
        state.data.examReportTasks = payload
    },
    // 学员报告page
    SETEXAMREPORTSPAGE(state, page) {
        page && setPage(state.ui.page, page)
    },
    // 组织报告page
    SETEORGANIZATIONPAGE(state, page) {
        page && setPage(state.ui.ePage, page)
    },
    // 任务page
    SETETASKPAGE(state, page) {
        page && setPage(state.ui.eTaskPage, page)
    },
    SETEXAMNUMBERLISTPAGE(state, page) {
        page && setPage(state.ui.numberPage, page)
        console.log(state.ui.numberPage)
    },
    SETEXCITATIONPAGE(state, page, filter) {
        page && setPage(state.ui.citationPage, page)
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
